package br.com.digicade.geosite.gpsavl.web.filter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class CustomHttpServletRequest extends HttpServletRequestWrapper {

    private Map<String, String> customHeaderMap = null;
    //private Map<String, String[]> customParameterMap = null;

    public CustomHttpServletRequest(HttpServletRequest request) {
        super(request);
        customHeaderMap = new HashMap<String, String>();
        //customParameterMap = new HashMap<String, String[]>();
    }

    public void addHeader(String name, String value) {
        customHeaderMap.put(name, value);
    }

    @Override
    public String getHeader(String name) {
        String paramValue = super.getHeader(name);
        if (paramValue == null) {
            paramValue = customHeaderMap.get(name);
        }
        return paramValue;
    }

    @Override
    public Enumeration<String> getHeaderNames() {
        List<String> names = Collections.list(super.getHeaderNames());
        names.addAll(customHeaderMap.keySet());
        return Collections.enumeration(names);
    }

    @Override
    public Enumeration<String> getHeaders(String name) {
        String value = customHeaderMap.get(name);
        if (value != null) {
            List<String> values = new ArrayList<String>();
            values.add(value);
            return Collections.enumeration(values);
        } else {
            return super.getHeaders(name);
        }
    }
}
